<script lang="ts" setup>
import { ref, h } from "vue";
import { getList, download, deleteData } from "@/api/merchant/management";
import { ReVxeGrid } from "@/components/ReVxeTable";
import CreateModal from "./components/IndexModal.vue";
import { VxeGridPropTypes } from "vxe-table";
import { VxeButton, VxeText } from "vxe-pc-ui";
import { ElMessage } from "element-plus";
import VxeUITable from "vxe-table";
import { useRouter } from "vue-router";
const router = useRouter();
const reVxeGridRef = ref();
let taxSourcesId = Number(sessionStorage.getItem("taxSourcesId"));
let merchantsInfoId = Number(sessionStorage.getItem("merchantsInfoId"));
const columns: VxeGridPropTypes.Columns<any> = [
  // { type: "checkbox", title: "", width: 60, align: "center" },
  {
    title: "签约批次号",
    field: "contractNumber",
    minWidth: 100
  },
  {
    title: "协议单名称",
    field: "contractName",
    showOverflow: true,
    minWidth: 200
  },
  {
    title: "通过/全部",
    field: "signingMobile",
    minWidth: 100,
    slots: {
      default: ({ row }) => {
        return [
          h(
            "span",
            {
              style: {
                color: "#67C23A"
              }
            },
            `${row.successNumber}/${row.shouldNumber}`
          )
        ];
      }
    }
  },
  {
    title: "状态",
    field: "stateDisplay",
    minWidth: 100
  },
  {
    title: "创建时间",
    field: "createTime",
    minWidth: 100
  },
  {
    title: "操作",
    field: "operation",
    minWidth: 100,
    slots: {
      default: ({ row }) => {
        return [
          h(
            VxeButton,
            {
              type: "text",
              status: "primary",
              onClick() {
                router.push({
                  path: "/management/audit-sign-detail",
                  query: {
                    id: row.id
                  }
                });
              }
            },
            () => "查看"
          ),
          h(VxeButton, {
            status: "primary",
            mode: "text",
            size: "mini",
            content: "撤回",
            async onClick() {
              const type = await VxeUI.modal.confirm("确认是否撤回项目！");
              // if (type == "confirm") {
              //   editStatusMerchant({
              //   id: row.id,
              //   state: row.state
              //   }).then((res) => {
              //     if(res){
              //     ElMessage.success(res.msg);
              //     handleSearch();
              //   }
              //   });
              // }

              // }
            }
          }),
          h(
            VxeButton,
            {
              type: "text",
              status: "danger",
              onClick: () => handleDelete(row)
            },
            () => "下载协议"
          )
        ];
      }
    }
  }
];
const formRef = ref();

const handleInitialFormParams = () => ({
  taxSourcesId: taxSourcesId,
  merchantsInfoId: merchantsInfoId,
  averageUserName: "",
  identityNumber: "",
  mobile: "",
  signedOrNot: null
});
const formItems = [
  {
    field: "importBatchNumber", // 确保与后端字段一致
    title: "",
    span: 4,
    itemRender: { name: "$input", props: { placeholder: "请输入签约批次号" } }
  },
  {
    field: "signedOrNot", // 确保与后端字段一致
    title: "",
    span: 4,
    itemRender: {
      name: "$select",
      props: {
        placeholder: "请选择批次状态",
        options: [
          { label: "待上传", value: "-1" },
          { label: "待审核", value: "0" },
          { label: "审核中", value: "1" },
          { label: "审核完成", value: "2" }
        ]
      }
    }
  },
  {
    field: "timeRang",
    title: "",
    span: 6,
    itemRender: {},
    slots: { default: "timeRang" }
  },
  {
    span: 6,
    itemRender: {
      name: "$buttons",
      children: [
        {
          props: {
            type: "submit",
            icon: "vxe-icon-search",
            content: "查询",
            status: "primary"
          }
        },
        { props: { type: "reset", icon: "vxe-icon-undo", content: "重置" } }
      ]
    }
  }
];
const formData = ref<{
  taxSourcesId: number;
  merchantsInfoId: number;
  averageUserName: string;
  identityNumber: string;
  mobile: string;
  signedOrNot: number | null;
}>(handleInitialFormParams());

const handleSearch = () => {
  reVxeGridRef.value.loadData();
};

const handleReset = () => {
  formData.value = handleInitialFormParams();
};

const createModalRef = ref();
const handleAdd = () => {
  createModalRef.value.showAddModal();
};
const handleEdit = (record: Recordable) => {
  createModalRef.value.showEditModal(record);
};

const handleDelete = async (record: Recordable) => {
  const type = await VxeUITable.modal.confirm("您确定要删除吗？");
  if (type == "confirm") {
    deleteData(record.id).then(() => {
      handleSearch();
    });
  }
};

const handleView = (record: Recordable) => {
  createModalRef.value.showViewModal(record);
};
const functions: Record<string, string> = {
  // add: "merchant.project.list.add",
  // edit: "merchant.project.template.edit",
  view: "merchant.management.index.view",
  delete: "merchant.management.index.delete"
};
</script>
<template>
  <div>
    <el-card :shadow="`never`">
      <vxe-form
        ref="formRef"
        :data="formData"
        :items="formItems"
        @submit="handleSearch"
        @reset="handleReset"
      >
        <template #timeRang="{ data }">
          <el-date-picker
            v-model="data.timeRang"
            clearable
            style="width: 100%"
            type="daterange"
            range-separator="-"
            value-format="YYYY-MM-DD"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </template>
      </vxe-form>
    </el-card>
    <el-card :shadow="`never`" class="table-card">
      <ReVxeGrid
        ref="reVxeGridRef"
        :request="getList"
        :functions="functions"
        :searchParams="formData"
        :columns="columns"
        @handleAdd="handleAdd"
        @handleView="handleView"
        @handleDelete="handleDelete"
      />
    </el-card>
    <CreateModal ref="createModalRef" @reload="handleSearch" />
  </div>
</template>
